package de.tum.in.eist.poll.client;

import java.util.List;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.pub.service.PollService;
import de.tum.in.eist.poll.pub.service.PollServiceAsync;
import de.tum.in.eist.poll.pub.shared.Feedback;
import de.tum.in.eist.poll.pub.shared.Lecture;
import de.tum.in.eist.poll.pub.shared.Student;
import de.tum.in.eist.poll.pub.shared.User;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class EIST_POLL implements EntryPoint {

	/**
	 * This is the entry point method.
	 */

	private PollServiceAsync server = GWT.create(PollService.class);
	private Label loginLabel = new Label("Enter your data");
	private TextBox loginText = new TextBox();
	private PasswordTextBox pwText = new PasswordTextBox();
	private Button loginButton = new Button("Login");

	public void onModuleLoad() {

		// Komponenten zur Page hinzufuegen

		RootPanel.get("header").add(loginLabel);
		RootPanel.get("header").add(loginText);
		RootPanel.get("header").add(pwText);
		RootPanel.get("header").add(loginButton);

		loginText.setFocus(true);

		// Actionlistener fuer loginButton definiern
		loginButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				checkData();
			}
		});
	}

	public void checkData() {
		String name = loginText.getText();
		String password = pwText.getText();
		server.login(name, password, new AsyncCallback<User>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Server Connection problems !");
			}

			@Override
			public void onSuccess(User result) {
				if (result == null) {
					Window.alert("Login failed!\nCheck Username and password");
				} else {
					if (result instanceof Student) {
						loadPollInterface(result);
					}
				}
			}

		});

		// ToDo : Daten vom server holen
		// ToDo : Daten vergleichen

	}

	private void loadPollInterface(User user) {
		// Aktueller Benutzer
		final User actUser = user;

		// Seite leeren
		RootPanel.get("header").clear();
		String groupname = "radButtonGruppe";
		// Umfrage laden

		// Erste frage : IW hats gefallen
		VerticalPanel vP1 = new VerticalPanel();
		Label frage1 = new Label("Wie hat ihnen die Vorlesung gefallen ?");
		final RadioButton a1 = new RadioButton(groupname);
		final RadioButton a2 = new RadioButton(groupname);
		final RadioButton a3 = new RadioButton(groupname);
		final RadioButton a4 = new RadioButton(groupname);
		final RadioButton a5 = new RadioButton(groupname);

		a1.setText("Very unsatisfied");
		a2.setText("Unsatisfied");
		a3.setText("Neutral");
		a4.setText("Satisfield");
		a5.setText("Very satisfied");
		vP1.add(frage1);
		vP1.add(a1);
		vP1.add(a2);
		vP1.add(a3);
		vP1.add(a4);
		vP1.add(a5);

		// Frage 2 : Was am besten gefallen hat
		VerticalPanel vP2 = new VerticalPanel();
		Label frage2 = new Label("What did you like most ?");
		final TextBox texta2 = new TextBox();
		vP2.add(frage2);
		vP2.add(texta2);

		VerticalPanel vP3 = new VerticalPanel();
		Label frage3 = new Label(
				"What did you dislike or did not understand at all ?");
		final TextBox texta3 = new TextBox();

		vP3.add(frage3);
		vP3.add(texta3);

		Button submitButton = new Button();
		submitButton.setText("abschicken");

		submitButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				String text1 = null;
				if (a1.getValue()) {
					text1 = a1.getText();
				} else if (a2.getValue()) {
					text1 = a2.getText();
				} else if (a3.getValue()) {
					text1 = a3.getText();
				} else if (a4.getValue()) {
					text1 = a4.getText();
				} else if (a5.getValue()) {
					text1 = a5.getText();
				} else {
					text1 = "Keine Angabe";
				}
				final String finalText1 = text1;
				final String text2 = texta2.getText();
				final String text3 = texta3.getText();
				server.requestLectures(actUser,
						new AsyncCallback<List<Lecture>>() {
							@Override
							public void onSuccess(List<Lecture> result) {
								FeedbackClient feedback = new FeedbackClient(
										finalText1, text2, text3,
										(Student) actUser, result.get(0));
								// Hole Liste von allen Vorlesungen
								server.submitFeedback(feedback,
										new AsyncCallback<String>() {

											@Override
											public void onSuccess(String result) {
												Window.alert(result);
											}

											@Override
											public void onFailure(
													Throwable caught) {
												caught.printStackTrace();
												Window.alert("Failed!");

											}
										});
							}

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Failed");

							}
						});

			}
		});

		RootPanel.get("header").add(vP1);
		RootPanel.get("header").add(vP2);
		RootPanel.get("header").add(vP3);
		RootPanel.get("header").add(submitButton);
	}
}
